import api from '../../../api/index';
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userid: null,
    authorUser: null,
    pageNum: 0,
    pageSize: 30,
    articleList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var { userid } = options
    this.setData({
      userid
    })
    console.log(userid);
    this.getauthorUser(userid)
    this.article(userid)
  },
  getauthorUser(id) {
    console.log(id);
    try {
      api.get(`/api/hxhealth/forum/author/info/${id}`).then((res) => {
        console.log(res);
        // res[0].images = JSON.parse(res[0].media).images
        // res[0].zanusers = res[0].zanusers?JSON.parse(res[0].zanusers):''
        this.setData({
          authorUser: res[0]
        })
        // this.commentList(res[0].id)
        // console.log(res[0]);
        // Toast.clear();
      });
    } catch (e) {
      console.error(e);
    }
  },
  article(id) {
    const actions = {
      data: {
        userid: id
      },
      head: {
        firstresult: this.data.pageNum,
        maxresults: this.data.pageSize,
        bodytype: 'flat',
      }
    };
    try {
      api.post('/api/hxhealth/forum/article/author', actions).then((res) => {
        console.log(res);
        var articleList = res
        for (let j = 0; j < articleList.length; j++) {
          if (articleList[j].media)
            articleList[j].media = JSON.parse(articleList[j].media)
        }
        console.log(articleList);
        this.setData({
          articleList
        })
      });
    } catch (e) {
      console.error(e);
    }
  },
  // 预览图片
  previewImage(e) {
    console.log(e);
    let { sources, index } = e.currentTarget.dataset
    sources.filter(i => {
      console.log(i);
      i.type = i.type == 2 ? 'video' : 'image'
      i.url = i.file
    })
    console.log(
      sources
    );
    wx.previewMedia({
      sources,
      current: index
    })
  },

  // 视频加载完成，改变视频的宽高
  videometa(e) {
    var that = this;
    //获取系统信息
    wx.getSystemInfo({
      success(res) {
        //视频的高
        var height = e.detail.height;

        //视频的宽
        var width = e.detail.width;

        //算出视频的比例
        var proportion = height / width;

        //res.windowWidth为手机屏幕的宽。
        var windowWidth = res.windowWidth;

        //算出当前宽度下高度的数值
        height = proportion * windowWidth;
        that.setData({
          height,
          width: windowWidth
        });
      }
    })
  },

  // 图片加载后判断图片宽高比例
  oneImageLoad(e) {
    const { width, height } = e.detail
    height >= width && this.setData({ isHeightMode: true })
  },
  // 关注
  onFollowClick() {
    try {
      api.get(`/api/hxhealth/forum/author/follow/${this.data.userid}`).then((res) => {
        console.log(res);
        Toast.success('关注成功');
        this.getauthorUser(this.data.userid)
        this.article(this.data.userid)
      });
    } catch (e) {
      console.error(e);
    }
  },
  // 已关注
  onUnFollowClick() {
    try {
      api.get(`/api/hxhealth/forum/author/unfollow/${this.data.userid}`).then((res) => {
        console.log(res);
        Toast.success('取消成功');
        this.getauthorUser(this.data.userid)
        this.article(this.data.userid)
      });
    } catch (e) {
      console.error(e);
    }
  },
  // 文章点赞、取消
  onclickzans(e) {
    var item = e.currentTarget.dataset.item
    if(item.zaned == 'false') {
      try {
        api.get(`/api/hxhealth/forum/article/zan/${item.id}`).then((res) => {
          console.log(res);
          Toast.success('点赞成功');
          this.getauthorUser(this.data.userid)
          this.article(this.data.userid)
        });
      } catch (e) {
        console.error(e);
      }
    } else {
      try {
        api.get(`/api/hxhealth/forum/article/unzan/${item.id}`).then((res) => {
          console.log(res);
          Toast.success('取消成功');
          this.getauthorUser(this.data.userid)
          this.article(this.data.userid)
        });
      } catch (e) {
        console.error(e);
      }
    }
  },
  // 文章收藏、取消
  onclickcollects(e) {
    var item = e.currentTarget.dataset.item
    if(item.collected == 'false') {
      try {
        api.get(`/api/hxhealth/forum/article/collect/${item.id}`).then((res) => {
          console.log(res);
          Toast.success('收藏成功');
          this.getauthorUser(this.data.userid)
          this.article(this.data.userid)
        });
      } catch (e) {
        console.error(e);
      }
    } else {
      try {
        api.get(`/api/hxhealth/forum/article/uncollect/${item.id}`).then((res) => {
          console.log(res);
          Toast.success('取消成功');
          this.getauthorUser(this.data.userid)
          this.article(this.data.userid)
        });
      } catch (e) {
        console.error(e);
      }
    }
  },
  // 查看文章详情
  godescPage(e) {
    console.log(e);
    var id = e.currentTarget.dataset.id
    wx.navigateTo({ url: `/community/pages/desc/index?id=${id}` });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})